﻿using LM.InterfacesFabricas.DTOs;
using LM.InterfacesFabricas.Fabricas;
using LM.InterfacesFabricas.Interfaces.Servicos;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;

namespace LM.Tela.Desktop.Formularios
{
    public partial class frmTabelaPreco : FormBase
    {
        public frmTabelaPreco()
        {
            InitializeComponent();
            ConfigureKeyDown();
        }

        private void ConfigureKeyDown()
        {
            AdicioneEventEnterToTAB( txtCodigo );
            AdicioneEventEnterToTAB( txtNome );
            AdicioneEventEnterToTAB( txtDataInicial );
            AdicioneEventEnterToTAB( txtDataFinal );
        }

        private void btnNovo_Click( object sender, EventArgs e )
        {
            PrepareTelaParaNovoRegistro();
        }

        private void PrepareTelaParaNovoRegistro()
        {
            LimpeCamposEdicao();
            CarregueItensJaCadastrados();
            txtCodigo.Enabled = true;
            txtCodigo.Focus();
        }

        private void CarregueItensJaCadastrados()
        {
            grdTabelaPreco.DataSource = FabricaGenerica.Crie<IServicoTabelaPreco>().ConsulteTodos();
        }

        private void LimpeCamposEdicao()
        {
            txtCodigo.Text = string.Empty;
            txtNome.Text = string.Empty;
            txtDataInicial.Text = "__/__/____";
            txtDataFinal.Text = "__/__/____";
        }

        private void btnSalvar_Click( object sender, EventArgs e )
        {
            Salvar();
        }

        private void Salvar()
        {
            int codigo;
            int.TryParse( txtCodigo.Text, out codigo );

            DateTime dataInicial;
            DateTime.TryParse( txtDataInicial.Text, out dataInicial );

            DateTime dataFinal;
            DateTime.TryParse( txtDataFinal.Text, out dataFinal );

            var tabelaPreco = new DtoTabelaPreco
            {
                Codigo = codigo,
                Nome = txtNome.Text,
                DataInicial = dataInicial,
                DataFinal = dataFinal
            };

            var servicoDeTabelaPreco = FabricaGenerica.Crie<IServicoTabelaPreco>();

            if ( EmEdicao() )
                servicoDeTabelaPreco.Atualize( tabelaPreco );
            else
                servicoDeTabelaPreco.Cadastre( tabelaPreco );

            PrepareTelaParaNovoRegistro();
        }

        private bool EmEdicao()
        {
            return !string.IsNullOrWhiteSpace( txtCodigo.Text ) && txtCodigo.Enabled == false;
        }

        private void btnExcluir_Click( object sender, EventArgs e )
        {
            Excluir();
        }

        private void Excluir()
        {
            if ( !EmEdicao() )
            {
                this.ExibaInconsistencia( "Tabela de Preço não selecionada para edição." );
                return;
            }

            FabricaGenerica.Crie<IServicoTabelaPreco>().Exclua( int.Parse( txtCodigo.Text ) );
            PrepareTelaParaNovoRegistro();
        }

        private void txtDataInicial_TypeValidationCompleted( object sender, TypeValidationEventArgs e )
        {
            ValidaData( sender, e );
        }

        private void txtDataFinal_TypeValidationCompleted( object sender, TypeValidationEventArgs e )
        {
            ValidaData( sender, e );
        }

        private void ValidaData( object sender, TypeValidationEventArgs e )
        {
            if ( !e.IsValidInput )
                this.ExibaInconsistencia( "Data inválida." );
        }

        private void grdTabelaPreco_CellDoubleClick( object sender, DataGridViewCellEventArgs e )
        {
            if ( e.RowIndex < 0 )
                return;

            var tabelaPreco = ( ( IList<DtoTabelaPreco> )grdTabelaPreco.DataSource )[e.RowIndex];

            txtCodigo.Text = tabelaPreco.Codigo.ToString( CultureInfo.InvariantCulture );
            txtCodigo.Enabled = false;
            txtNome.Text = tabelaPreco.Nome;
            txtDataInicial.Text = tabelaPreco.DataInicial.ToString();
            txtDataFinal.Text = tabelaPreco.DataFinal.ToString();
        }

        private void frmTabelaPreco_Load( object sender, EventArgs e )
        {
            PrepareTelaParaNovoRegistro();
        }

        private void btnItens_Click( object sender, EventArgs e )
        {
            frmItemTabelaPreco itens = new frmItemTabelaPreco();
            itens.ShowDialog();
        }
    }
}
